//
//  AppDelegate.m
//  IngCard
//
//  Created by kf on 2017/4/28.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "AppDelegate.h"
#import "QMUIConfigurationTemplate.h"
#import "UITabBarItem+Factory.h"
#import "YDPeripheralsViewController.h"
#import "YDProfileViewController.h"
#import "YDMessageViewController.h"
#import "PPNetworkHelper.h"
#import "YDAccountManager.h"
#import "YDUserGuideViewController.h"
#import "YDTheme.h"
#import "YDUITabBarViewController.h"
#import "YDMyCardViewController.h"
#import "YDMyContactsViewController.h"
#import "YDAccountManager.h"
#import "YDContactsManager.h"
#import "YDActivityManager.h"

@interface AppDelegate ()
@property (nonatomic, strong) YDUITabBarViewController *tabbarController;
@property(nonatomic, strong) QMUINavigationController *userGuideNavigationController;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [QMUIConfigurationTemplate setupConfigurationTemplate];
    [QMUIConfigurationManager renderGlobalAppearances];
    
    [[YDTheme sharedInstance] render];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userLogin)
                                                 name:ACCOUNT.Notification_userLogin
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userLoginOut)
                                                 name:ACCOUNT.Notification_userLoginOut
                                               object:nil];
    
    [self showWindow];
    
    return YES;
}

#pragma mark - Private

- (void)showWindow
{
    if (ACCOUNT.account.login_token && ACCOUNT.account.uid)
    {   
        [ACCOUNT getAccountDetail:^(BOOL success, NSString *msg) {
            [self showTab:success];
        }];
    }
    else
    {
        [self showTab:NO];
    }
    
    
}

- (void) userLogin
{
    [self showTab:YES];
}

- (void) userLoginOut
{
    [self showTab:NO];
}

-(void) showTab:(BOOL) isTab
{
    if (isTab)
    {
        for (QMUINavigationController *navigationController in self.tabbarController.viewControllers) {
            [navigationController popToRootViewControllerAnimated:NO];
        }
        
        self.window.rootViewController = self.tabbarController;
        [self.tabbarController setSelectedIndex:0];
        [self.window makeKeyAndVisible];
    }
    else
    {
        [self.userGuideNavigationController popToRootViewControllerAnimated:NO];
        self.window.rootViewController = self.userGuideNavigationController;
        [self.window makeKeyAndVisible];
    }
}

# pragma mark - Lazy Load

- (YDUITabBarViewController *)tabbarController
{
    if (!_tabbarController)
    {
        _tabbarController = [[YDUITabBarViewController alloc] init];
        
        // QMUIKit
        YDMyCardViewController *homeViewController = [[YDMyCardViewController alloc] init];
        homeViewController.hidesBottomBarWhenPushed = NO;
        QMUINavigationController *homeNavController = [[QMUINavigationController alloc] initWithRootViewController:homeViewController];
        
        homeNavController.tabBarItem = [UITabBarItem tabBarItemWithTitle:@"名片卡"
                                                                   image:UIImageMake(@"tabbar_card")
                                                           selectedImage:UIImageMake(@"tabbar_card")
                                                                     tag:0];
        
        
        YDMyContactsViewController *contactsViewController = [[YDMyContactsViewController alloc] init];
        contactsViewController.hidesBottomBarWhenPushed = NO;
        contactsViewController.title = @"联系人";
        QMUINavigationController *contactsNavController = [[QMUINavigationController alloc] initWithRootViewController:contactsViewController];
        
        contactsNavController.tabBarItem = [UITabBarItem tabBarItemWithTitle:@"联系人"
                                                                      image:UIImageMake(@"tabbar_contact")
                                                              selectedImage:UIImageMake(@"tabbar_contact")
                                                                        tag:1];
        
        
        YDMessageViewController *messageViewController = [[YDMessageViewController alloc] init];
//        YDPeripheralsViewController *messageViewController = [YDPeripheralsViewController new];
        messageViewController.hidesBottomBarWhenPushed = NO;
        messageViewController.title = @"消息";
        QMUINavigationController *discoverNavController = [[QMUINavigationController alloc] initWithRootViewController:messageViewController];
        
        discoverNavController.tabBarItem = [UITabBarItem tabBarItemWithTitle:@"消息"
                                                                       image:UIImageMake(@"tabbar_message")
                                                               selectedImage:UIImageMake(@"tabbar_message")
                                                                         tag:2];
        
        
        YDProfileViewController *profileViewController = [[YDProfileViewController alloc] init];
        profileViewController.hidesBottomBarWhenPushed = NO;
        QMUINavigationController *profileNavController = [[QMUINavigationController alloc] initWithRootViewController:profileViewController];
        
        profileNavController.tabBarItem = [UITabBarItem tabBarItemWithTitle:@"我的"
                                                                      image:UIImageMake(@"tabbar_my")
                                                              selectedImage:UIImageMake(@"tabbar_my")
                                                                        tag:3];
        // window root controller
        _tabbarController.viewControllers = @[homeNavController,contactsNavController, discoverNavController,profileNavController];
    
    }
    return _tabbarController;
}

-(QMUINavigationController *) userGuideNavigationController
{
    if (!_userGuideNavigationController)
    {
        YDUserGuideViewController *userGuideVC = [[YDUserGuideViewController alloc] init];
        _userGuideNavigationController = [[QMUINavigationController alloc] initWithRootViewController:userGuideVC];
    }
    return _userGuideNavigationController;
}

-(UIWindow *) window
{
    if (!_window)
    {
        _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    }
    return _window;
}
@end
